import jsonTV from "../utils/tv.js"
interface openWindow{
    val:string
    urls?:any    
}
class Playtv{
    jsontv:Array<object>=jsonTV
    listEle!:HTMLElement
    frameEle!: HTMLIFrameElement
    parseBtn!: HTMLButtonElement
    frameSrc?:string
    videoUrl:HTMLInputElement
    sourceWeb!:HTMLElement
    sourceURL:string=''
    tvindex:number=0
    openWinBtn:HTMLButtonElement
    constructor(frameSrc?:string){
        this.frameSrc=frameSrc
        this.listEle=document.getElementById('vip_ul')!
        this.frameEle=document.getElementById('myiframe') as HTMLIFrameElement
        this.parseBtn=document.getElementById('start_fla') as HTMLButtonElement
        this.videoUrl=document.getElementById('videourl') as HTMLInputElement
        this.sourceWeb=document.getElementById('sourceWeb') as HTMLIFrameElement
        this.openWinBtn=document.getElementById('openWin') as HTMLButtonElement
    }
    init(){
        console.log('jsonTV',this.jsontv);
        this.addList();
    }
    addList(){//添加左侧LI内容
        let _this=this;
        let tmpl=(content:any)=>`
            ${content.map((item: { name: string })=>`
                <li>${item.name}</li>
            `).join('')}
        `;
        _this.listEle.innerHTML=tmpl(_this.jsontv);
        setTimeout(()=>{
            this.getList();
        },500)
    }
    getList(){//获取左侧的LI
        let _this=this;
        let mylist:NodeList=document.querySelectorAll('#vip_ul li');
        mylist.forEach((item,index)=>{         
            //给每个li绑定事件
            item.addEventListener('click',function(){
                _this.sourceWeb.innerHTML=_this.jsontv[index]['urls'];//显示视频来源地址
                _this.openWin('none');//隐藏打开新标签页按钮
                _this.setFrameSrc(index); 
            })            
        })            
    }
    setFrameSrc(index:number=0){//更改iframe的src地址
        let _this=this;
        _this.frameSrc='';
        if(_this.videoUrl.value.length>0){
            _this.parseBtn.addEventListener('click',function(){                
                _this.frameSrc=_this.jsontv[index]['urls']+''+_this.videoUrl.value
                _this.sourceWeb.innerHTML=_this.frameSrc;//视频地址
                _this.frameEle.src=_this.frameSrc;
                _this.openWin('block',_this.frameSrc);
            })
        }        
    }
    openWin(val:string,urls?:any):void{//显示打开新标签页按钮
        let _this=this;
        _this.openWinBtn.style.display=val
        //window.open(urls,'_blank');
        if(urls!=''){            
            _this.openWinBtn.addEventListener('click',function(){
                window.location.href=urls
            })            
        }        
    }
}
export default Playtv
